package my.db.DTO;
import java.io.*;
import java.nio.channels.FileChannel;

import org.apache.tomcat.util.http.fileupload.FileItemStream;
public class FileUpload {	
	public static String ImagePath="C:/Users/PC1/Desktop/articlemanagnement/Article_Management/WebContent/upload";
	public static boolean processfile(String path,FileItemStream item) throws IOException{
		String p;
		File fi=new File(ImagePath);
		if(!fi.exists()){
			fi.mkdir();
		}
		p=fi.getAbsolutePath()+File.separator+item.getName();
		File savedFile=new File(fi.getAbsolutePath()+File.separator+item.getName());
		FileOutputStream fos=new FileOutputStream(savedFile);
		InputStream is=item.openStream();
		int x=0;
		byte[] b=new byte[1024];
		while((x=is.read(b))!=-1){
			fos.write(b,0,x);
		}
		fos.flush();
		fos.close();
		//copyFile(p,item.getName());
		return true;
	}
	public static void copyFile(String str,String filename) throws IOException {
	    FileChannel inputChannel = null;
	    FileChannel outputChannel = null;
	    File source=new File(str);
	    File dest=new File("Images"+File.separator+filename+".jpg");
	    System.out.println(filename);
	   
	    try {
	        inputChannel = new FileInputStream(source).getChannel();
	        outputChannel = new FileOutputStream(dest).getChannel();
	        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
	    } finally {
	        inputChannel.close();
	        outputChannel.close();
	    }
	}
}
